package com.xundao.yxt.repository.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xundao.yxt.domain.model.Permission;
import com.xundao.yxt.domain.repository.PermissionRepository;
import com.xundao.yxt.repository.converter.PermissionConverter;
import com.xundao.yxt.repository.entity.PermissionEntity;
import com.xundao.yxt.repository.mapper.PermissionMapper;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Repository;

@Slf4j
@Repository
@AllArgsConstructor
public class PermissionRepositoryImpl implements PermissionRepository {
    private final PermissionMapper permissionMapper;
    private final PermissionConverter permissionConverter;
    @Override
    public Permission getPermissionByPermissionId(String permissionId) {
        Wrapper<PermissionEntity> wrapper = new LambdaQueryWrapper<>(PermissionEntity.class)
                .eq(PermissionEntity::getPermissionId, permissionId);
        PermissionEntity permissionEntity = permissionMapper.selectOne(wrapper);
        return permissionConverter.convert(permissionEntity);
    }

    @Override
    public int addPermission(Permission permission) {
        PermissionEntity permissionEntity = permissionConverter.convert(permission);
        return permissionMapper.insert(permissionEntity);
    }
}
